package com.rjwh.dingdong.client.activity.loginregist;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.guide.MainActivity;
import com.rjwh.dingdong.client.adapter.RelationShipGridAdapter;
import com.rjwh.dingdong.client.application.AppManager;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.RelationItemData;
import com.rjwh.dingdong.client.bean.localbean.SysLoginBabylistBean;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.AddBabyBoundCompletePresenter;
import com.rjwh.dingdong.client.mvp.view.AddBabyBoundCompleteView;
import com.rjwh.dingdong.client.util.ToastUtil;
import com.rjwh.dingdong.client.widget.RoundImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Kay
 * @ClassName AddBabyBoundCompleteActivity
 * @Description
 * @date 2016-7-15
 * @history 2016-7-15 author: description:
 */
public class AddBabyBoundCompleteActivity extends BaseActivity implements AddBabyBoundCompleteView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.add_baby_bound_complete_img)
    RoundImageView addBabyBoundCompleteImg;
    @BindView(R.id.add_baby_bound_complete_name_tv)
    TextView addBabyBoundCompleteNameTv;
    @BindView(R.id.add_baby_bound_complete_account_tv)
    TextView addBabyBoundCompleteAccountTv;
    @BindView(R.id.add_baby_bound_complete_relationship_tv)
    TextView addBabyBoundCompleteRelationshipTv;

    private List<RelationItemData> list;
    private int adapterPosition = -1;
    private String relationName;
    private String childname;
    private String childid;
    private String childavatar;

    private AddBabyBoundCompletePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_baby_bound_complete_layout);
        ButterKnife.bind(this);
        parseIntent();
        initRelationshipData();
        initView();
        presenter = new AddBabyBoundCompletePresenter(this, this);
    }

    private void initView() {
        initToolbarHelper();
        if (!AsStrUtils.isEmpty(childname)) {
            addBabyBoundCompleteNameTv.setText("关联宝宝 : " + childname);
        }
        if (!AsStrUtils.isEmpty(childid)) {
            addBabyBoundCompleteAccountTv.setText("宝宝ID : " + childid);
        }
        if (childavatar != null) {
            Glide.with(this).load(childavatar).dontAnimate().placeholder(R.drawable.icon_defult_avatar).error(R.drawable.icon_defult_avatar).into(addBabyBoundCompleteImg);
        }

    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            childname = intent.getStringExtra("childname");
            childid = intent.getStringExtra("childid");
            childavatar = intent.getStringExtra("childavatar");
        }
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        getSupportActionBar().setTitle("绑定宝宝");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }


    @OnClick({R.id.add_baby_bound_complete_relationship_root, R.id.add_baby_conform_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_baby_bound_complete_relationship_root:
                showRelationShip();
                break;
            case R.id.add_baby_conform_btn:
                doAddBaby();
                break;
        }
    }

    private void doAddBaby() {
        if (AsStrUtils.isEmpty(childid)) {
            return;
        }
        if (AsStrUtils.isEmpty(relationName)) {
            ToastUtil.showToast(this, "您还没有选择和宝宝的关系哦");
            return;
        }
        presenter.addBaby(childid, "", "", "", relationName);
    }

    /**
     * 设置关系
     */
    private void showRelationShip() {
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        alertDialog.setView(inflater.inflate(R.layout.dialog_baby_info_relationship, null));
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.dialog_baby_info_relationship);
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        GridView relationship_gv = (GridView) window.findViewById(R.id.dialog_baby_info_relationship_gv);
        Button relationship_conform_btn = (Button) window.findViewById(R.id.dialog_baby_info_relationship_conform_btn);
        Button relationship_other_btn = (Button) window.findViewById(R.id.dialog_baby_info_relationship_other_btn);
        RelationShipGridAdapter relationShipGridAdapter = new RelationShipGridAdapter(this, addBabyBoundCompleteRelationshipTv.getText().toString());
        relationship_gv.setAdapter(relationShipGridAdapter);
        relationShipGridAdapter.appendToList(list);
        relationship_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RelationShipGridAdapter adapter = (RelationShipGridAdapter) parent.getAdapter();
                if (adapterPosition != position) {
                    adapterPosition = position;
                }
                adapter.setSelected(adapterPosition);
                RelationItemData item = (RelationItemData) adapter.getItem(position);
                relationName = item.getName();
                adapter.notifyDataSetChanged();

            }

        });

        relationship_conform_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                addBabyBoundCompleteRelationshipTv.setText(relationName);
            }
        });
        relationship_other_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                showOtherRelationshipDailog();
            }
        });
    }

    private void showOtherRelationshipDailog() {
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        alertDialog.setView(inflater.inflate(R.layout.dialog_person_data_nick_name, null));
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.dialog_person_data_nick_name);
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        final EditText nick_name_et = (EditText) window.findViewById(R.id.dialog_person_data_nick_name_et);
        nick_name_et.setHint("输入和孩子的关系");
        ImageView nick_name_img = (ImageView) window.findViewById(R.id.dialog_person_data_nick_name_img);
        Button nick_name_conform_btn = (Button) window.findViewById(R.id.dialog_person_data_nick_name_conform_btn);
        Button nick_name_cancel_btn = (Button) window.findViewById(R.id.dialog_person_data_nick_name_cancel_btn);
        nick_name_img.setImageResource(R.drawable.icon_baby_info_relation_big);
        nick_name_conform_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nameInput = nick_name_et.getText().toString();
                if (!AsStrUtils.isEmpty(nameInput)) {
                    alertDialog.dismiss();
                    relationName = nameInput;
                    addBabyBoundCompleteRelationshipTv.setText(nameInput);
                } else {
                    ToastUtil.showToast(AddBabyBoundCompleteActivity.this, "你还没有输入内容");
                }

            }
        });
        nick_name_cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

    }

    private void initRelationshipData() {
        list = new ArrayList<>();
        RelationItemData relationItemDataBaba = new RelationItemData();
        relationItemDataBaba.setName("爸爸");
        relationItemDataBaba.setPicNomal(R.drawable.icon_relationship_baba_normal);
        relationItemDataBaba.setPicChecked(R.drawable.icon_relationship_baba_checked);
        RelationItemData relationItemDataMama = new RelationItemData();
        relationItemDataMama.setName("妈妈");
        relationItemDataMama.setPicNomal(R.drawable.icon_relationship_mama_normal);
        relationItemDataMama.setPicChecked(R.drawable.icon_relationship_mama_checked);
        RelationItemData relationItemDataYeye = new RelationItemData();
        relationItemDataYeye.setName("爷爷");
        relationItemDataYeye.setPicNomal(R.drawable.icon_relationship_yeye_normal);
        relationItemDataYeye.setPicChecked(R.drawable.icon_relationship_yeye_checked);
        RelationItemData relationItemDataNainai = new RelationItemData();
        relationItemDataNainai.setName("奶奶");
        relationItemDataNainai.setPicNomal(R.drawable.icon_relationship_nainai_normal);
        relationItemDataNainai.setPicChecked(R.drawable.icon_relationship_nainai_checked);
        RelationItemData relationItemDataWaigong = new RelationItemData();
        relationItemDataWaigong.setName("外公");
        relationItemDataWaigong.setPicNomal(R.drawable.icon_relationship_waigong_normal);
        relationItemDataWaigong.setPicChecked(R.drawable.icon_relationship_waigong_checked);
        RelationItemData relationItemDataWaipo = new RelationItemData();
        relationItemDataWaipo.setName("外婆");
        relationItemDataWaipo.setPicNomal(R.drawable.icon_relationship_waipo_normal);
        relationItemDataWaipo.setPicChecked(R.drawable.icon_relationship_waipo_checked);
        list.add(relationItemDataBaba);
        list.add(relationItemDataMama);
        list.add(relationItemDataYeye);
        list.add(relationItemDataNainai);
        list.add(relationItemDataWaigong);
        list.add(relationItemDataWaipo);
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    public void loadData(SysLoginBabylistBean babyobj) {
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYID, babyobj.getBabyid());
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYNAME, babyobj.getBabyname());
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYSEX, babyobj.getSex());
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYBIRTHDAY, babyobj.getBirthday());
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_USERCHENGWEI, babyobj.getTitle());
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYAVATAR, babyobj.getBabyavatar());
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYAGE, babyobj.getBabyage());
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_USERCOMPLETE, "1");
        AppManager.getAppManager().finishAllActivity();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
